package org.wso2.carbon.registry.custom.skb.services;

import org.wso2.carbon.registry.common.services.RegistryAbstractAdmin;
import org.wso2.carbon.registry.core.Association;
import org.wso2.carbon.registry.custom.skb.beans.SKBBean;
import org.wso2.carbon.registry.custom.skb.util.SKBUtil;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Resource;

public class SKBService extends RegistryAbstractAdmin{

    public SKBBean getSKBBean(String path) throws Exception {
        return SKBUtil.getSKBBean(path);
    }

    public void addColletion(String sPath) throws Exception{
        Registry registry = getRootRegistry();
        Collection collection = registry.newCollection();
        registry.put(sPath, collection);

    }

    public void addResource(String sPath, String content) throws Exception{
        Registry registry = getRootRegistry();
        Resource r1 = registry.newResource();
        r1.setMediaType("text/plain");
        r1.setContent(content.getBytes());
        registry.put(sPath, r1);

    }

    public void addProblemResource(String sPath, String content) throws Exception{
        Registry registry = getRootRegistry();
        Resource r1 = registry.newResource();
        r1.setMediaType("skb");

        r1.setContent(content.getBytes());
        registry.put(sPath, r1);

    }

    public void addAssociation(String sPath, String dPath, String type) throws Exception{
        Registry registry = getRootRegistry();
        registry.addAssociation(sPath, dPath, type);
        
    }

    public void applyTag(String sPath, String tag) throws Exception{
        Registry registry = getRootRegistry();
        registry.applyTag(sPath, tag);
    }


    public String getResurceWithContent(String path) throws Exception{
        Registry registry = getRootRegistry();
        Resource r1 = registry.get(path);
        if (r1 instanceof Collection) {
//            r1.get
            return null;
        }
        byte[] contentBytes = (byte[])r1.getContent();
        String content = new String(contentBytes);
        return content;

    }

    public String[] getCollectionWithChildren(String path) throws Exception{
        String[] emptyString = null;
        Registry registry = getRootRegistry();
        Resource r1 = registry.get(path);
        if (r1 instanceof Collection) {
        Collection collection = (Collection)r1;
        emptyString = collection.getChildren();

        }
//      else if  if(registry.get(path, 0, 25).){}
        if(r1.equals(null)){
        
        }
        return emptyString;


    }


    public String[] getAllAssociationForAResource(String sPath) throws Exception{
        Registry registry = getRootRegistry();
        Association association[] = registry.getAllAssociations(sPath);
        String asso[]=null;
        for(int i=0;i<association.length;i++){
            asso[i] = association.toString();

        }
        return asso;
    }

}
